#include "commonpage.h"
#include "ui_commonpage.h"

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    //取消水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //
    connect(ui->playAllbtn,&QPushButton::clicked,this,[=](){
        emit playAll(pageType);
    });
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        qDebug()<<"发射播放信号";
        emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &image)
{
    ui->pageTitle->setText(title);//设置标题
    ui->musicImageLabel->setPixmap(QPixmap(image));//设置封面

    ui->musicImageLabel->setScaledContents(true);
    //添加listitemBox测试
    ListItemBox* listItemBox = new ListItemBox(this);
    QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
    //listWidgetItem->setSizeHint(listItemBox->sizeHint()); // 让 item 匹配 widget 的大小
    listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 40));
    ui->pageMusicList->setItemWidget(listWidgetItem,listItemBox);



}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicOfPage.clear();
    for(auto music:musicList)
    {
        switch (pageType)
        {
            case PageType::LIKE_PAGE:
                if(music.getIsLike())
                {
                    musicOfPage.push_back(music.getMusicId());
                }
            break;
            case PageType::LOCAL_PAGE:
                musicOfPage.push_back(music.getMusicId());
            break;
            case PageType::HISTORY_PAGE:
                if(music.getIsHistory())
                {
                    musicOfPage.push_back(music.getMusicId());
                }
            break;
        default:
            qDebug()<<"暂不支持~";
        }
    }
}

void CommonPage::addMusicToPlayList(MusicList &musiclist)
{
    (void)musiclist;
    ;//
}

void CommonPage::reFresh(MusicList &musicList)
{
    //从musicList中分离出当前页面的所有音乐
    ui->pageMusicList->clear();

    //添加新的音乐
    addMusicToMusicPage(musicList);
    //遍历歌单，将歌单中的所有歌曲显示到界面
    for(auto musicId:musicOfPage)
    {
        auto it = musicList.findMusicByMusicId(musicId);
        if(it == musicList.end())
            continue;
        ListItemBox* listItemBox = new ListItemBox(this);

        //设置列表音乐的歌曲名，歌手，专辑
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setMusicSinger(it->getMusicSinger());
        listItemBox->setMusicAlbum(it->getMusicAlbum());
        listItemBox->setLikeMusic(it->getIsLike());

        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(listItemBox->width(),listItemBox->height()));
        ui->pageMusicList->setItemWidget(item,listItemBox);

        //接受listItemBox发射的信号
        connect(listItemBox,&ListItemBox::setIsLike,this,[=](bool isLike){
           emit updateLikeMusic(isLike,it->getMusicId());
        });
    }
    repaint();
}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto music:musicList)
    {
        switch(pageType)
        {
            case LOCAL_PAGE:
                playList->addMedia(music.getMusicUrl());
            break;
            case LIKE_PAGE:
                if(music.getIsLike())
                {
                    playList->addMedia(music.getMusicUrl());
                }
            break;
        case HISTORY_PAGE:
                if(music.getIsHistory())
                {
                    playList->addMedia(music.getMusicUrl());
                }
            break;
        default:
            qDebug()<<"不支持喵~";
            break;
        }

    }

}

void CommonPage::addMusicToPlaylist(MusicList &musiclist, QMediaPlaylist *playlist)
{
    for(auto music:musiclist)
    {
        switch (pageType) {
            case LOCAL_PAGE:
                playlist->addMedia(music.getMusicUrl());
            break;
            case LIKE_PAGE:
                if(music.getIsLike())
                {
                    playlist->addMedia(music.getMusicUrl());
                }
            break;
            case HISTORY_PAGE:
                if(music.getIsHistory())
                {
                    playlist->addMedia(music.getMusicUrl());
                }
            break;
        default:
            qDebug()<<"不支持~";
        }
    }
}

const QString CommonPage::getMusicIdByIndex(int index) const
{
    if(index>=musicOfPage.size())
    {
        qDebug()<<"无此歌~";
        return "";
    }
    return musicOfPage[index];
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}
